package com.bank.bank.account.service.impl;

import com.bank.bank.account.mapper.AccountMapper;
import com.bank.bank.account.pojo.dto.AccountAddParam;
import com.bank.bank.account.pojo.entity.Account;
import com.bank.bank.account.pojo.vo.AccountTransactionVO;
import com.bank.bank.account.service.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 1.添加账户业务
     * @param accountAddParam
     * @return
     */
    @Override
    public Account addAccount(AccountAddParam accountAddParam) {
        log.debug("添加账户业务:accountAddParam:{}", accountAddParam);
        Account account = new Account();
        BeanUtils.copyProperties(accountAddParam, account);
        //设置余额为:0.00
        account.setBalance(0.00);
        account.setCreatedAt(new Date());
        //处理卡号: 时间戳 + 四位随机数[1000-9999];
        long timestamp = System.currentTimeMillis();
        int random = new Random().nextInt(9000) + 1000; //1000-9999
        String accountNumber = "" + timestamp + random;
        account.setAccountNumber(accountNumber);
        //调用接口,插入数据
        accountMapper.insertAccount(account);
        //id已处理: <insert id="insertAccount" useGeneratedKeys="true" keyProperty="id">
        return account;
    }

    /**
     * 2.查询账户列表业务
     * @return
     */
    @Override
    public List<Account> listAccounts() {
        List<Account> accountList = accountMapper.selectAllAccount();
        return accountList;
    }

    /**
     * 3.注销账户业务
     * @param accountId
     */
    @Override
    public void delAccount(Long accountId) {
        log.debug("注销账户业务:accountId:{}", accountId);
        accountMapper.deleteAccountById(accountId);
    }

    /**
     * 4.查询账户交易详情
     * @param accountId 账户id
     * @return
     */
    @Override
    public AccountTransactionVO getAccountTransactions(Integer accountId) {
        log.debug("查询账户交易详情业务:accountId:{}", accountId);
        AccountTransactionVO accountTransactionVO = accountMapper.selectAccountTransactionByAccountId(accountId);
        return accountTransactionVO;
    }
}







